package core.cards.election.gui;

import java.awt.Frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import core.cards.CardResultFrontend;
import core.cards.election.model.ElectionBackend;
import core.cards.election.model.ElectionResultBackend;
import core.cards.election.model.ElectionResultSubBackend;
import core.cards.election.model.VotingCard;
import core.cards.election.model.VotingCardOption;

public class ElectionResultFrontend extends CardResultFrontend {

	protected ElectionResultFrontend(Composite parent, ElectionResultBackend electionResultBackend, ElectionBackend electionBackend) {
		super(parent, SWT.NONE);

		setLayout(new GridLayout(1, false));

		TabFolder tbFldTypes = new TabFolder(this, SWT.NONE);
		tbFldTypes.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		for (ElectionResultSubBackend electionResultSubBackend : electionResultBackend.getElectionResultSubBackendList()) {
			new TabItemResultType(tbFldTypes, electionResultSubBackend, electionBackend);
		}

	}

	class TabItemResultType extends TabItem {

		public TabItemResultType(TabFolder parent, ElectionResultSubBackend electionResultSubBackend, ElectionBackend electionBackend) {
			super(parent, SWT.NONE);

			setText(electionResultSubBackend.getName());

			TabFolder tbFldrCards = new TabFolder(parent, SWT.NONE);
			setControl(tbFldrCards);

			for (VotingCard votingCard : electionBackend.getVotingCardList()) {
				new TabItemTypeCard(tbFldrCards, electionResultSubBackend, votingCard);
			}
		}

		@Override
		protected void checkSubclass() {
			// Disable the check that prevents subclassing of SWT components
		}
	}

	class TabItemTypeCard extends TabItem {
		// private VotingCard votingCard;

		public TabItemTypeCard(TabFolder parent, ElectionResultSubBackend electionResultSubBackend, VotingCard votingCard) {
			super(parent, SWT.NONE);

			Composite cmpResult = new Composite(parent, SWT.EMBEDDED);
			setControl(cmpResult);
			cmpResult.setLayout(new GridLayout(1, false));
			Frame chartFrame = SWT_AWT.new_Frame(cmpResult);

			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			for(VotingCard electionVotingCard : electionResultSubBackend.getVoteResult().getVotingCardList()) {
				if(electionVotingCard.getName().compareTo(votingCard.getName()) == 0) {
					for (VotingCardOption votingCardOption : electionVotingCard.getVotingCardOptionList()) {
						dataset.setValue(votingCardOption.getValue(), "Votes", votingCardOption.getName());
					}
				}
			}
			JFreeChart chart = ChartFactory.createBarChart("Election outcome", "Cadidates", "Votes", dataset, PlotOrientation.VERTICAL, false, true, false);
			CategoryPlot plot = (CategoryPlot)chart.getPlot();
	        CategoryAxis xAxis = (CategoryAxis)plot.getDomainAxis();
	        xAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
			ChartPanel cp = new ChartPanel(chart);
			chartFrame.add(cp);

			setText(votingCard.getName());
		}

		@Override
		protected void checkSubclass() {
			// Disable the check that prevents subclassing of SWT components
		}
	}
}
